//
//  ViewController.m
//  CoreData_通讯录
//
//  Created by z on 15/11/20.
//  Copyright © 2015年 z. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"
#import "AddViewController.h"
#import "DetailViewController.h"



@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>

@property(nonatomic, strong) UISearchBar *searchBar;
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSArray *personArr;
@property(nonatomic, strong) NSMutableArray *searchArr;


@end

#pragma mark - 重用标志
static NSString *const cellIndetigier = @"reuse";

@implementation ViewController
{
    bool isSearched;
}
- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.searchArr = [NSMutableArray array];
    isSearched = NO;

    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"通讯录";
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 64, ViewWidth, 64)];
    [self.view addSubview:self.searchBar];
    self.searchBar.delegate = self;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click:)];
    
    [self creatTableView];
    self.personArr = [NSArray array];
    self.personArr = [Person MR_findAll];
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.personArr = nil;
    self.personArr = [Person MR_findAll];
    isSearched = NO;

    [self.tableView reloadData];
}


// 页面跳转 进行添加
- (void)click:(UIBarButtonItem *)button{

    AddViewController *addVC = [[AddViewController alloc] init];
    [addVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentViewController:addVC animated:YES completion:^{
    
    }];
}



// 创建tableView
- (void)creatTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 128, ViewWidth, ViewHeight - 128) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorColor = [UIColor blackColor];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIndetigier];
    [self.view addSubview:self.tableView];

}



#pragma mark -- tableView协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (isSearched) {
        return self.searchArr.count;
    } else{
        
        return self.personArr.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (isSearched) {
        cell.textLabel.text = [[self.searchArr[indexPath.row] lastObject] name];
        
    } else {
        cell.textLabel.text = [self.personArr[indexPath.row] name];
    }
    return cell;
}



// 进行左划删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(!isSearched){
        Person *person = self.personArr[indexPath.row];
        // 进行删除
        [person MR_deleteEntity];

        // 进行保存
        [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
        self.personArr = [Person MR_findAll];
    
        [self.tableView reloadData];
    }
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController *detalVC = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:detalVC animated:YES];
    detalVC.person = self.personArr[indexPath.row];
}





#pragma mark -- searchBar 协议
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.searchArr removeAllObjects];
    NSArray *arr = [Person MR_findAll];
      for (Person *per in arr) {
     
        if ([[per name] hasPrefix:searchBar.text]) {
             NSArray * arr = [Person MR_findByAttribute:@"name" withValue:per.name];
            [self.searchArr addObject:arr];
            }
    }
    isSearched = YES;
    [self.tableView reloadData];
    
}



- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if ([searchBar.text isEqualToString:@""]) {
        isSearched = NO;
        [self.tableView reloadData];
    }
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
